﻿$(function () {
	//初始化datatable
	$("#table").XRDataTable({
		ajaxUrl: "Admin/Paper/getDatas",
		paramFunc: paramFunc,
		operation: {
			"edit": ["Admin/Paper/edit/#id","编辑"],
			"icon-trash": [deleteModule,"删除"],
			"icon-ok": [changeStatus, "提交审核"],
			// "icon-check": [changeSubmitKh, "提交考核库"],
			"icon-legal": ['Admin/Paper/auditView/#id', "审核进程"]
		},
		hasCheckbox: false,
		afterFunc: afterFunc
	});

	$('.cart-c-title-icon').click(function () {
		$('.cart-c').animate({ right: "-400px" }, function () {
		});
	});
	resetCartPosition();
	$(window).resize(function () {
		resetCartPosition();
	});
});

var paramFunc = function () {
	var data = getFormJson();
	return data;
};

var afterFunc = function () {
	// alert(JSON.stringify(trData));
	$("#table tbody tr").each(function () {
		var data = $("#table").getOpts().datas[$(this).index()];
		// console.log(data)
		if (data.is_audit) {
			$(this).find('#icon-ok').show();
			$(this).find('#icon-trash').show();
		}
		else {
			$(this).find('#icon-ok').hide();
			$(this).find('#icon-trash').hide();
		}
		if (data.is_edit) {
			$(this).find('#fa-edit').show();
		}
		else {
			$(this).find('#fa-edit').hide();
		}
	
		if (data.status == $("#unSubmit").val()) {
			$(this).find('#icon-legal').hide();
		}
		if (data.change_status == '99') {
			$(this).addClass('tr-red');
		} else if (data.change_status == '3') {
			$(this).addClass('tr-green');
		}

		if(data.is_submit_kh == 0){
			$(this).find('#icon-check').show();
		}else{
			$(this).find('#icon-check').hide();
		}


	});
	//二维码放大
	imgBig('erweima img');
}

function resetCartPosition() {
	$('.cart-c').height(Number($(window).height()) - 55);
	$('.cart-c-box').height(Number($(window).height()) - 105);
}


function exportData() {
	var load_alert = loadAlert('数据统计中...');
	$.ajax({
		type: "post",
		url: "Admin/Paper/getDatas/isExport/true",
		async: true,
		dataType: 'json',
		data: $('#searchForm').serialize(),
		success: function (data) {
			closeAlert(load_alert);
			if (data.status) {
				window.location.href = data.v;
			} else {
				window.parent.topMessagePrompt('error', data.msg);
			}
		}
	});
}

var deleteModule = function (idx, trData) {
	var msg = "是否确认删除【" + trData.name + "】?";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function (isConfirm) {
		if (isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/Paper/delete/" + trData.id,
				async: true,
				success: function (data) {
					var ret = $.parseJSON(data);
					if (ret.status) {
						window.parent.topMessagePrompt('success', ret.msg);
						window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error', ret.msg);
					}
				}
			});
		}
	});
}

var changeStatus = function (idx, trData) {
	var msg = "是否提交审核?";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function (isConfirm) {
		if (isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/Paper/submit_audit/" + trData.id,
				async: true,
				success: function (data) {
					var ret = $.parseJSON(data);
					if (ret.status) {
						window.parent.topMessagePrompt('success', ret.msg);
						window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error', ret.msg);
					}
				}
			});
		}
	});
}

//是否提交考核库
// var changeSubmitKh = function(idx,trData){
// 	var msg = "考核只能提交一次,是否需要提交考核库?";
// 	swal({
// 		title: "请确认",
// 		text: msg,
// 		type: "warning",
// 		showCancelButton: true,
// 		closeOnConfirm: true,
// 		confirmButtonText: "确定",
// 		cancelButtonText: '取消',
// 		confirmButtonColor: "#ec6c62"
// 	}, function (isConfirm) {
// 		if (isConfirm) {
// 			$.ajax({
// 				type: "post",
// 				url: "Admin/Paper/submit_kh/" + trData.id,
// 				async: true,
// 				success: function (data) {
// 					var ret = $.parseJSON(data);
// 					if (ret.status) {
// 						window.parent.topMessagePrompt('success', ret.msg);
// 						window.parent.refreshTable();
// 					} else {
// 						window.parent.topMessagePrompt('error', ret.msg);
// 					}
// 				}
// 			});
// 		}
// 	});
// }


function importExcel() {
	let file = document.getElementById("title").files; // js 获取文件对象
	if (!file) {
		window.parent.topMessagePrompt('error', "请先上传文件");
		if (d != undefined)
			d.close().remove();
		return;
	}
	let formData = new FormData();
	for (i = 0; i < file.length; i++) {
		// formData.append("file[" + i + "]", file[i])
		formData.append("file", file[i])
	}
	let data = formData;

	$.ajax({
		url: 'Admin/Paper/importExcel',
		data: data,
		type: "POST",
		dataType: "json",
		cache: false, //上传文件无需缓存
		processData: false, //用于对data参数进行序列化处理 这里必须false
		contentType: false, //必须
		success: function (ret) {
			if (ret.status) {
				let a = JSON.parse(ret.v)
				if (a.length == 0) {
					window.parent.topMessagePrompt('success', ret.msg);
					location.reload();
				} else {
					let error = '';
					// a.map(value => { error += `第${value}行,` })
					$.each(a, function (k, v) {  error += '第'+v+'行'; });
					error += '作者不存在!'
					window.parent.topMessagePrompt('success', error);
					location.reload();
				}
			} else {
				window.parent.topMessagePrompt('error', ret.msg);
				location.reload();
			}
		}
	})
	// var load_alert = loadAlert('导入中...');
}
